package com.egoo.ticket.server.controller;

import com.egoo.ticket.server.dao.EwsDepartmentDao;
import com.egoo.ticket.server.dao.WorkOrderFilterMenuDao;
import com.egoo.ticket.server.pojo.dto.*;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.entity.FilterFirst;
import com.egoo.ticket.server.pojo.entity.WorkOrderFilterMenu;
import com.egoo.ticket.server.pojo.vo.*;
import com.egoo.ticket.server.service.*;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @author Donny_dong
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/workOrderFilterMenu", produces = "application/json")
@Api(value = "自定义过滤器菜单管理",tags = "自定义过滤器菜单管理",description = "自定义过滤器菜单管理")
public class WorkOrderFilterMenuController {

	@Autowired
	private WorkOrderFilterMenuService workOrderFilterMenuService;

	@Autowired
	private FilterFirstService filterFirstService;

	@Autowired
	private FilterThirdService filterThirdService;

	@Autowired
	private WorkOrderFilterMenuDao workOrderFilterMenuDao;

	@Autowired
	private UserRoleService userRoleService;

	@Autowired
	private UserService userService;

	@Autowired
	EwsDepartmentDao ewsDepartmentDao;


	@ApiOperation(value = "获取所有的过滤器菜单信息", notes = "获取所有的过滤器菜单信息")
	@RequestMapping(method = RequestMethod.GET, value = "/selectAll")
	public DbResponse selectAll(@ApiParam(value = "当前页", required = true) @RequestParam("currentPage") Integer currentPage,
								@ApiParam(value = "每页行数", required = true) @RequestParam("pageSize") Integer pageSize,
								HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		PageOutputDto pageOutputDto = workOrderFilterMenuService.selectAll(currentPage, pageSize,tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(pageOutputDto);
		return dbResponse;
	}

	@ApiOperation(value = "根据id获取过滤器菜单信息", notes = "根据id获取过滤器菜单信息")
	@RequestMapping(method = RequestMethod.GET, value = "/selectById")
	public DbResponse selectById(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Integer id,
								 HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuService.selectById(id,tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(workOrderFilterMenu);
		return dbResponse;
	}

	@ApiOperation(value = "根据菜单名称和工单类型获取过滤器菜单信息", notes = "根据菜单名称和工单类型获取过滤器菜单信息")
	@RequestMapping(method = RequestMethod.POST, value = "/selectByNameAndOrderTypeName")
	public DbResponse selectByNameAndOrderTypeName(@ApiParam(value = "selectByNameAndOrderTypeName", required = true) @RequestBody SelectByNameAndTypeDto selectByNameAndTypeDto,
												   HttpServletRequest request) {

		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		// 模糊查询参数
		String name = selectByNameAndTypeDto.getName();
		// 工单类型名称
		String workOrderTypeId = selectByNameAndTypeDto.getWorkOrderTypeId();
		Integer currentPage = selectByNameAndTypeDto.getCurrentPage();
		Integer pageSize = selectByNameAndTypeDto.getPageSize();
		if (pageSize == null) {
			return DbResponse.getDbResponse(ResponseConstant.RES_FAIL_CODE, "分页参数不能为空！", null);
		}
		if (currentPage == null) {
			return DbResponse.getDbResponse(ResponseConstant.RES_FAIL_CODE, "分页参数不能为空！", null);
		}
		try {
			return workOrderFilterMenuService.selectByNameAndOrderTypeName(name, workOrderTypeId, currentPage, pageSize,tenantId);
		}catch (Exception e){
			log.info("根据菜单名称和工单类型获取过滤器菜单信息异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "根据菜单名称和工单类型获取过滤器菜单信息异常："+e);
		}

	}

	@ApiOperation(value = "提供默认状态排序值", notes = "提供默认状态排序值")
	@RequestMapping(value = "/selectOrder", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public DbResponse selectOrder(HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		int retCode = workOrderFilterMenuService.selectOrderMax(tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(retCode);
		return dbResponse;
	}

	@ApiOperation(value = "新建过滤器菜单", notes = "新建过滤器菜单")
	@RequestMapping(method = RequestMethod.POST, value = "/createWorkOrderFilterMenu")
	public DbResponse createWorkOrderFilterMenu(@ApiParam(value = "workOrderFilterMenu", required = true) @RequestBody WorkOrderFilterMenuInputDto workOrderFilterMenuInputDto,
												HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "新建失败");
		String filterName = workOrderFilterMenuInputDto.getName();
		Long workOrderTypeId = workOrderFilterMenuInputDto.getWorkOrderTypeId();
		if(StringUtils.isBlank(filterName) || null == filterName){
			dbResponse.setMsg("过滤器名称不能为空");
			return dbResponse;
		}else {
			if(filterName.length()> ConstantCode.ControllerStrLengthCheck.LENGTH){
				dbResponse.setMsg("过滤器名称不能超过80个字");
				return dbResponse;
			}
			WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuDao.selectByEqualsName(filterName, workOrderTypeId,tenantId);
			if (!Objects.isNull(workOrderFilterMenu)){
				dbResponse.setMsg("该过滤器已存在");
				return dbResponse;
			}
		}
		VisibleRangeVo visibleRange = workOrderFilterMenuInputDto.getVisibleRange();
		String visibleRangeType = visibleRange.getType();
		List<Object> visibleRangeValue = visibleRange.getValue();
		//判断可见范围
		if(null == visibleRangeType){
			dbResponse.setMsg("菜单可见范围不能为空");
			return dbResponse;
		}else {
			if (!visibleRangeType.equals(ConstantCode.UserRoleDeptType.ALL)){
				if (visibleRangeValue.size() == 0){
					dbResponse.setMsg("菜单可见范围不能为空");
					return dbResponse;
				}
			}
		}
		if (null != workOrderFilterMenuInputDto.getFilterCondition()) {
			FilterConditionVo filterCondition = workOrderFilterMenuInputDto.getFilterCondition();
			List<ConditionVo> conditionList = filterCondition.getCondition();
			for (int i = 0; i < conditionList.size(); i++) {
				Integer firstId = conditionList.get(i).getFirstId();
				Integer secondId = conditionList.get(i).getSecondDataId();
				String typeId =  conditionList.get(i).getTypeId();
				//condition.value 保存需要过滤的内容  用户主键（字符串）、用户主键（字符串集合）  或角色，部门等（扩展需要替换内容）
				Object value = conditionList.get(i).getValue();
				if (null == firstId || null == secondId || null == value) {
					dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
					dbResponse.setMsg("筛选条件填写不完整，请补充完整");
					return dbResponse;
				} else {
					FilterFirst filterFirst = filterFirstService.selectById(firstId);
					String name = filterFirst.getName();
					if (firstId == ConstantCode.FilterFirst.WORK_ORDER_CREATOR_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_CURRENT_RECEIVER_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_HISTORY_RECEIVER_ID) {
						if (null == typeId) {
							dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
							dbResponse.setMsg("筛选条件填写不完整，请补充完整");
							return dbResponse;
						}else {
//							if(3 == secondId || 4 == secondId){
//								try {
//									String value1 = (String) value;
//								} catch (Exception e) {
//									dbResponse.setRetCode(StatusCode.RET_ERROR);
//									dbResponse.setMsg(name + "填写的数据格式不正确");
//									return dbResponse;
//								}
//							}else {
////								List value2 = (List) value;
//								try {
//									List value2 = (List) value;
//								} catch (Exception e) {
//									dbResponse.setRetCode(StatusCode.RET_ERROR);
//									dbResponse.setMsg(name + "填写的数据格式不正确");
//									return dbResponse;
//								}
//							}
						}

					}
					if (firstId == ConstantCode.FilterFirst.WORK_ORDER_URGE_COUNT_ID) {
						try {
							Integer.parseInt(value.toString());
//						if (secondId == 1 || secondId == 2) {
//							Integer.parseInt(value.toString());
//						} else {
//							List valueList = (List) value;
//							for (int j = 0; j < valueList.size(); j++) {
//								Integer.parseInt(valueList.get(j).toString());
//							}
//						}
						} catch (Exception e) {
							dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
							dbResponse.setMsg(name + "只能填入整数，否则无法解析");
							return dbResponse;
						}
					}
					if (firstId == ConstantCode.FilterFirst.WORK_ORDER_CREATED_TIME_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_CLOSED_TIME_ID) {
						try {
							if (secondId == 1 || secondId == 2) {
								Double.parseDouble(value.toString());
							} else {
								List valueList = (List) value;
								for (int j = 0; j < valueList.size(); j++) {
									Double.parseDouble(valueList.get(j).toString());
								}
							}
						} catch (Exception e) {
							dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
							dbResponse.setMsg(name + "只能填入数字，否则无法解析");
							return dbResponse;
						}
					}
				}

//				if (firstId == 14) {
//					try {
//						Integer.parseInt(value.toString());
////						if (secondId == 1 || secondId == 2) {
////							Integer.parseInt(value.toString());
////						} else {
////							List valueList = (List) value;
////							for (int j = 0; j < valueList.size(); j++) {
////								Integer.parseInt(valueList.get(j).toString());
////							}
////						}
//					} catch (Exception e) {
//						dbResponse.setRetCode(StatusCode.RET_ERROR);
//						dbResponse.setMsg(name + "只能填入整数，否则无法解析");
//						return dbResponse;
//					}
//				}
//				if (firstId == 15 || firstId == 16) {
//					try {
//						if (secondId == 1 || secondId == 2) {
//							Double.parseDouble(value.toString());
//						} else {
//							List valueList = (List) value;
//							for (int j = 0; j < valueList.size(); j++) {
//								Double.parseDouble(valueList.get(j).toString());
//							}
//						}
//					} catch (Exception e) {
//						dbResponse.setRetCode(StatusCode.RET_ERROR);
//						dbResponse.setMsg(name + "只能填入数字，否则无法解析");
//						return dbResponse;
//					}
//				}
				//803 数据权限扩展

				//如果value是字符创
				if(value instanceof java.lang.String){
					String valueStr=String.valueOf(value);
					//当前用户
					if(valueStr.equals(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_KEY)){
						conditionList.get(i).setValue(userId);
					}
					//当前用户角色
					if(valueStr.equals(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_ROLE_KEY)){
						//当前用户的所有角色
						List<String> roles=userRoleService.selectByUserId(userId,tenantId);
						conditionList.get(i).setValue(roles);
					}
				}

				//如果value是List
				if(value instanceof java.util.List){
					List<List> doubleList =(List<List>) value;
					conditionList.get(i).setValue(doubleList);
//					String valueStr=((List<String>) value).get(0);
//					//当前用户部门
//					if(valueStr.equals(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_DEPT_KEY)){
//						//用户表保存的部门主键可能是第一级 也能第二级的id
//					      EwsUser user=userService.selectById(userId);
//					      String user_deptId=user.getDepartmentId();
//						  EwsDepartmentVo deptVo=ewsDepartmentDao.selectByPrimaryKey(user_deptId);
//						  //没有下级部门 直接保存部门主键字符串
//						  if("0".equals(deptVo.getPid())){
//							  conditionList.get(i).setValue(deptVo.getId());
//						  }else{
//						  	  List<String>  deptIdList= new ArrayList<>();
//							  EwsDepartmentVo parentDeptVo=ewsDepartmentDao.selectByPrimaryKey(deptVo.getPid());
//							  deptIdList.add(parentDeptVo.getId());
//							  deptIdList.add(deptVo.getId());
//							  conditionList.get(i).setValue(deptIdList);
//						  }
//					}
				}
			}
		}

//		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuService.selectByEqualsName(workOrderFilterMenuInputDto.getName(), workOrderFilterMenuInputDto.getWorkOrderTypeId());
//		if (workOrderFilterMenu != null) {
//			dbResponse.setRetCode(StatusCode.RET_ERROR);
//			dbResponse.setMsg("菜单名称已存在");
//			return dbResponse;
//		}
		WorkOrderFilterMenu workOrderFilterMenu1 = workOrderFilterMenuService.selectByOrdering(workOrderFilterMenuInputDto.getOrdering(),tenantId);
		if (null != workOrderFilterMenu1) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("状态排序已存在");
			return dbResponse;
		}
		if(CollectionUtils.isEmpty(workOrderFilterMenuInputDto.getFieldInfoList())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"请选择需要配置的表头信息");
		}
		workOrderFilterMenuInputDto.setTenantId(tenantId);
		int retCode = workOrderFilterMenuService.insertMenu(workOrderFilterMenuInputDto);

		dbResponse.setRetCode(200);
		dbResponse.setMsg("新建成功");

		return dbResponse;
	}

	@ApiOperation(value = "删除过滤器菜单", notes = "根据id删除过滤器菜单")
	@RequestMapping(value = "/deleteFilterMenu", method = RequestMethod.DELETE, produces = "application/json")
	@ResponseBody
	public DbResponse deleteFilterMenu(@ApiParam(value = "过滤器菜单id", required = true) @RequestParam("id") Integer id,
									   HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "删除失败");
		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuService.selectById(id,tenantId);
		if (null != workOrderFilterMenu) {
			if (!workOrderFilterMenu.getWorkOrderTypeId().equals(0)) {
				int retCode = workOrderFilterMenuService.deleteMenu(id,tenantId);
				if (retCode == ResponseConstant.RESULT_SUCCESS) {
					dbResponse.setRetCode(200);
					dbResponse.setMsg("删除成功");
				}
				return dbResponse;
			}
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("无法删除该过滤器菜单");
			return dbResponse;
		}
		dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
		dbResponse.setMsg("没有该过滤器菜单，无法删除");
		return dbResponse;
	}

	@ApiOperation(value = "编辑过滤器菜单", notes = "编辑过滤器菜单")
	@RequestMapping(method = RequestMethod.POST, value = "/updateContent")
	public DbResponse updateContent(@ApiParam(value = "workOrderFilterMenu", required = true) @RequestBody WorkOrderFilterMenuInputDto workOrderFilterMenuInputDto,
									HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "更新失败");
		String filterName = workOrderFilterMenuInputDto.getName();
		Integer id = workOrderFilterMenuInputDto.getId();
		Long workOrderTypeId = workOrderFilterMenuInputDto.getWorkOrderTypeId();
		if(StringUtils.isBlank(filterName) || null == filterName){
			dbResponse.setMsg("过滤器名称不能为空");
			return dbResponse;
		}else {
			if(filterName.length()> ConstantCode.ControllerStrLengthCheck.LENGTH){
				dbResponse.setMsg("过滤器名称不能超过80个字");
				return dbResponse;
			}
			WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuDao.selectByEqualsName(filterName, workOrderTypeId,tenantId);
			if (!Objects.isNull(workOrderFilterMenu)&& !workOrderFilterMenu.getId().equals(id)){
				dbResponse.setMsg("该过滤器名称已存在,更换后重试");
				return dbResponse;
			}
		}
		VisibleRangeVo visibleRange = workOrderFilterMenuInputDto.getVisibleRange();
		String visibleRangeType = visibleRange.getType();
		List visibleRangeValue = visibleRange.getValue();
		//判断可见范围
		if(null == visibleRangeType){
			dbResponse.setMsg("菜单可见范围不能为空");
			return dbResponse;
		}else {
			if (!visibleRangeType.equals(ConstantCode.UserRoleDeptType.ALL)){
				if (visibleRangeValue.size() == 0){
					dbResponse.setMsg("菜单可见范围不能为空");
					return dbResponse;
				}
			}
		}
		if (null != workOrderFilterMenuInputDto.getFilterCondition()) {
			FilterConditionVo filterCondition = workOrderFilterMenuInputDto.getFilterCondition();
			List<ConditionVo> conditionList = filterCondition.getCondition();
			for (int i = 0; i < conditionList.size(); i++) {
				Integer firstId = conditionList.get(i).getFirstId();
				Integer secondId = conditionList.get(i).getSecondDataId();
				String typeId = conditionList.get(i).getTypeId();
				Object value = conditionList.get(i).getValue();
				if (null == firstId || null == secondId || null == value) {
					dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
					dbResponse.setMsg("删选条件填写不完整，请补充完整");
					return dbResponse;
				} else {
					FilterFirst filterFirst = filterFirstService.selectById(firstId);
					String name = filterFirst.getName();
					if (firstId == ConstantCode.FilterFirst.WORK_ORDER_CREATOR_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_CURRENT_RECEIVER_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_HISTORY_RECEIVER_ID) {
						if (null == typeId) {
							dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
							dbResponse.setMsg("删选条件填写不完整，请补充完整");
							return dbResponse;
						}else {
//							if(3 == secondId || 4 == secondId){
//								try {
//									String value1 = (String) value;
//								} catch (Exception e) {
//									dbResponse.setRetCode(StatusCode.RET_ERROR);
//									dbResponse.setMsg(name + "填写的数据格式不正确");
//									return dbResponse;
//								}
//							}else {
////								List value2 = (List) value;
//								try {
//									List value2 = (List) value;
//								} catch (Exception e) {
//									dbResponse.setRetCode(StatusCode.RET_ERROR);
//									dbResponse.setMsg(name + "填写的数据格式不正确");
//									return dbResponse;
//								}
//							}
						}
					}
					if (firstId == ConstantCode.FilterFirst.WORK_ORDER_URGE_COUNT_ID) {
						try {
							Integer.parseInt(value.toString());
//						if (secondId == 1 || secondId == 2) {
//							Integer.parseInt(value.toString());
//						} else {
//							List valueList = (List) value;
//							for (int j = 0; j < valueList.size(); j++) {
//								Integer.parseInt(valueList.get(j).toString());
//							}
//						}
						} catch (Exception e) {
							dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
							dbResponse.setMsg(name + "只能填入整数，否则无法解析");
							return dbResponse;
						}
					}
					if (firstId == ConstantCode.FilterFirst.WORK_ORDER_CREATED_TIME_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_CLOSED_TIME_ID) {
						try {
							if (secondId == 1 || secondId == 2) {
								Double.parseDouble(value.toString());
							} else {
								List valueList = (List) value;
								for (int j = 0; j < valueList.size(); j++) {
									Double.parseDouble(valueList.get(j).toString());
								}
							}
						} catch (Exception e) {
							dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
							dbResponse.setMsg(name + "只能填入数字，否则无法解析");
							return dbResponse;
						}
					}
				}
			}
		}

		String name = workOrderFilterMenuInputDto.getName();

		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuService.selectById(id,tenantId);
		if (null != workOrderFilterMenu) {
			if (workOrderFilterMenu.getOrdering().equals(workOrderFilterMenuInputDto.getOrdering())) {
				workOrderFilterMenuInputDto.setName(name);
				workOrderFilterMenuInputDto.setOrdering(workOrderFilterMenuInputDto.getOrdering());
				workOrderFilterMenuInputDto.setTenantId(tenantId);
				Integer retCode = workOrderFilterMenuService.updateContent(workOrderFilterMenuInputDto);
				if (retCode == 1) {
					dbResponse.setRetCode(200);
					dbResponse.setMsg("编辑成功");
				}
				return dbResponse;
			}
			if (!workOrderFilterMenu.getOrdering().equals(workOrderFilterMenuInputDto.getOrdering())) {
				WorkOrderFilterMenu workOrderFilterMenu2 = workOrderFilterMenuService.selectByOrdering(workOrderFilterMenuInputDto.getOrdering(),tenantId);
				if (workOrderFilterMenu2 != null) {
					dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
					dbResponse.setMsg("状态排序已存在");
					return dbResponse;
				}
				workOrderFilterMenuInputDto.setName(name);
				workOrderFilterMenuInputDto.setOrdering(workOrderFilterMenuInputDto.getOrdering());
				workOrderFilterMenuInputDto.setTenantId(tenantId);
				int retCode = workOrderFilterMenuService.updateContent(workOrderFilterMenuInputDto);
				if (retCode == ResponseConstant.RESULT_SUCCESS) {
					dbResponse.setRetCode(200);
					dbResponse.setMsg("编辑成功");
				}
				return dbResponse;
			}
		}
		dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
		dbResponse.setMsg("没有该过滤器工单");
		return dbResponse;

	}

	@ApiOperation(value = "更新过滤器菜单的使用状态", notes = "更新过滤器菜单的使用状态")
	@RequestMapping(method = RequestMethod.GET, value = "/updateUseStatus")
	public DbResponse updateUseStatus(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Integer id,
									  @ApiParam(value = "userId", required = true) @RequestParam(name = "userId") String userId,
									  @ApiParam(value = "status", required = true) @RequestParam(name = "status") Integer status,

									HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);

			userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);

			DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "更新失败");
			WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuService.selectById(id, tenantId);
			if (workOrderFilterMenu == null) {
				dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
				dbResponse.setMsg("该过滤器菜单不存在");
				return dbResponse;
			}
			int retCode = workOrderFilterMenuService.updateUseStatus(id, userId, status, tenantId);
			if (retCode == 1) {
				dbResponse.setRetCode(200);
				dbResponse.setMsg("更新成功");
			}
			return dbResponse;

	}

//	@ApiOperation(value = "批量新建过滤器菜单", notes = "批量新建过滤器菜单")
//	@RequestMapping(method = RequestMethod.POST, value = "/createWorkOrderFilterMenus")
//	public DBResponse createWorkOrderFilterMenus(@ApiParam(value = "workOrderFilterMenu", required = true) @RequestBody List<WorkOrderFilterMenu> workOrderFilterMenu) {
//		DBResponse dbResponse = new DBResponse(StatusCode.RET_ERROR, "新建失败");
//		int retCode = workOrderFilterMenuService.insertByBatch(workOrderFilterMenu);
//		if (retCode >= 1) {
//			dbResponse.setRetCode(200);
//			dbResponse.setMsg("新建成功");
//		}
//		return dbResponse;
//	}

	@ApiOperation(value = "获取所有的第一列筛选条件", notes = "获取所有的第一列筛选条件")
	@RequestMapping(method = RequestMethod.GET, value = "/selectAllCondition")
	public DbResponse selectFirstCondition(HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		List<FilterFirst> filterFirstList = filterFirstService.selectAll(tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(filterFirstList);
		return dbResponse;
	}

	@ApiOperation(value = "获取所有的第二列筛选条件", notes = "获取所有的第二列筛选条件")
	@RequestMapping(method = RequestMethod.GET, value = "/selectSecondCondition")
	public DbResponse selectSecondCondition(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Integer id) {
		DbResponse dbResponse = new DbResponse();
		FilterSecondOutDto filterSecondOutDto = filterFirstService.selectByDto(id);
		dbResponse.setRetCode(200);
		dbResponse.setData(filterSecondOutDto);
		return dbResponse;
	}

	@ApiOperation(value = "获取筛选条件的第三部分", notes = "获取筛选条件的第三部分")
	@RequestMapping(method = RequestMethod.POST, value = "/selectThirdCondition")
	public DbResponse selectThirdCondition(@RequestBody FilterThirdInputDto filterThirdInputDto,
										   HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		Integer firstId = filterThirdInputDto.getFirstId();
		Integer secondDataId = filterThirdInputDto.getSecondDataId();
		String secondTypeId = (String) filterThirdInputDto.getSecondTypeId();
		Long workOrderTypeId = filterThirdInputDto.getWorkOrderTypeId();
		String name = filterThirdInputDto.getName();
		if(null == firstId){
			dbResponse.setRetCode(-1);
			dbResponse.setMsg("条件填写不完整，请补充完整");
			return dbResponse;
		}else {
			if(ConstantCode.FilterFirst.WORK_ORDER_TITLE_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_DESCRIBE_ID == firstId || ConstantCode.FilterFirst.CUSTOM_NAME_ID == firstId || ConstantCode.FilterFirst.CUSTOM_PHONE_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_URGE_COUNT_ID == firstId){
			}else {
				if(null == secondDataId){
					dbResponse.setRetCode(-1);
					dbResponse.setMsg("条件填写不完整，请补充完整");
					return dbResponse;
				}else {
					if(firstId == ConstantCode.FilterFirst.WORK_ORDER_NODE_ID){
						if(null == workOrderTypeId){
							dbResponse.setRetCode(-1);
							dbResponse.setMsg("条件填写不完整，请补充完整");
							return dbResponse;
						}
					}
					if(firstId == ConstantCode.FilterFirst.WORK_ORDER_CREATOR_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_CURRENT_RECEIVER_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_HISTORY_RECEIVER_ID){
						if(null == secondTypeId){
							dbResponse.setRetCode(-1);
							dbResponse.setMsg("条件填写不完整，请补充完整");
							return dbResponse;
						}
					}
				}
			}
		}
		filterThirdInputDto.setTenantId(tenantId);
		FilterThirdOutDto filterThirdOutDto = filterThirdService.selectByDto(filterThirdInputDto);
		dbResponse.setRetCode(200);
		dbResponse.setData(filterThirdOutDto);
		return dbResponse;
	}


	@ApiOperation(value = "根据筛选条件获取工单信息", notes = "根据筛选条件获取工单信息")
	@RequestMapping(method = RequestMethod.GET, value = "/selectByCondition")
	public DbResponse selectByCondition(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Integer id,
										@ApiParam(value = "userId", required = true) @RequestParam(name = "userId") Integer userId) {
		DbResponse dbResponse = new DbResponse();
//		List<WorkOrder> workOrderList = workOrderFilterMenuService.selectByDynamic(id, userId);
//		dbResponse.setRetCode(200);
//		dbResponse.setData(workOrderList);
		return dbResponse;
	}


	@ApiOperation(value = "获取筛选条件的第三部分针对当前用户", notes = "获取筛选条件的第三部分针对当前用户")
	@RequestMapping(method = RequestMethod.POST, value = "/selectThirdConditionForCurrentUser")
	public DbResponse selectThirdConditionForCurrentUser(@RequestBody FilterThirdInputDto filterThirdInputDto,
														 HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		Integer firstId = filterThirdInputDto.getFirstId();
		Integer secondDataId = filterThirdInputDto.getSecondDataId();
		String secondTypeId = filterThirdInputDto.getSecondTypeId().toString();
		Long workOrderTypeId = filterThirdInputDto.getWorkOrderTypeId();
		String name = filterThirdInputDto.getName();
		if(null == firstId){
			dbResponse.setRetCode(-1);
			dbResponse.setMsg("条件填写不完整，请补充完整");
			return dbResponse;
		}else {
			if(ConstantCode.FilterFirst.WORK_ORDER_TITLE_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_DESCRIBE_ID == firstId || ConstantCode.FilterFirst.CUSTOM_NAME_ID == firstId || ConstantCode.FilterFirst.CUSTOM_PHONE_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_URGE_COUNT_ID == firstId){
			}else {
				if(null == secondDataId){
					dbResponse.setRetCode(-1);
					dbResponse.setMsg("条件填写不完整，请补充完整");
					return dbResponse;
				}else {
					if(firstId == ConstantCode.FilterFirst.WORK_ORDER_NODE_ID){
						if(null == workOrderTypeId){
							dbResponse.setRetCode(-1);
							dbResponse.setMsg("条件填写不完整，请补充完整");
							return dbResponse;
						}
					}
					if(firstId == ConstantCode.FilterFirst.WORK_ORDER_CREATOR_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_CURRENT_RECEIVER_ID || firstId == ConstantCode.FilterFirst.WORK_ORDER_HISTORY_RECEIVER_ID){
						if(null == secondTypeId){
							dbResponse.setRetCode(-1);
							dbResponse.setMsg("条件填写不完整，请补充完整");
							return dbResponse;
						}
					}
				}
			}
		}
		filterThirdInputDto.setTenantId(tenantId);
		FilterThirdOutDto filterThirdOutDto = filterThirdService.selectByDto(filterThirdInputDto);
		if (null !=filterThirdInputDto.getSecondTypeId()) {
			List dataList = filterThirdOutDto.getDataList();
			WorkOrderStatusDataVo tempVo = new WorkOrderStatusDataVo();
			//二级过滤条件的type是用户时 workOrderFilterMenu/selectThirdConditionForCurrentUser返回数据添加  “当前用户选项“
			if(ConstantCode.FilterUserAndRole.SECOND_TYPE_ID_USER.equals(filterThirdInputDto.getSecondTypeId())) {
				//之前版本  value 对 KEY  label对VALUE
				/*jsonObject.put("value", ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_KEY);
				jsonObject.put("label", ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_VALUE);*/
				tempVo.setValue(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_KEY);
				tempVo.setLabel(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_VALUE);
				dataList.add(tempVo);
			}
			//当前用户角色
			if(ConstantCode.FilterUserAndRole.SECOND_TYPE_ID_ROLE.equals(filterThirdInputDto.getSecondTypeId())) {
				tempVo.setValue(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_ROLE_KEY);
				tempVo.setLabel(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_ROLE_VALUE);
				dataList.add(tempVo);
			}
			//当前用户部门
			if(ConstantCode.FilterUserAndRole.SECOND_TYPE_ID_DEPT.equals(filterThirdInputDto.getSecondTypeId())) {
				tempVo.setValue(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_DEPT_KEY);
				tempVo.setLabel(ConstantCode.CurrentLoadUser.CURRENT_LOAD_USER_DEPT_VALUE);
				dataList.add(tempVo);
			}
			filterThirdOutDto.setDataList(dataList);
		}
		dbResponse.setRetCode(200);
		dbResponse.setData(filterThirdOutDto);
		return dbResponse;
	}

}
